package org.jsola.hr.common;

import org.jsola.hr.entity.EmpSocItemDO;
import org.jsola.hr.entity.SocDifferenceDO;
import org.jsola.hr.vo.EmpSocInfoListVO;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 社保总览导出
 */
public class ExportSocOverview {

    private static final String LATE_FEES = "lateFees";

    private static final String SERVICE_COST = "serviceCost";

    private static final String COMPANY = "企业";

    private static final String PERSONAL = "个人";

    private static final String SOC_STATS = "社保合计";

    private static final String PF_STATS = "公积金合计";

    private static final String SOC_PF_STATS = "社保公积金总合计";

    private static final String RECEIVABLES_STATS = "应收总合计";

    private static final String CARDINALITY = "基数";

    private static final String PERSONAL_PROPORTION = "个人缴纳比例";

    private static final String COMPANY_PROPORTION = "企业缴纳比例";

    private static final String PERSONAL_AMOUNT = "个人缴纳金额";

    private static final String COMPANY_AMOUNT = "企业缴纳金额";

    /**
     * 其他费用
     */
    public static String otherConversion(EmpSocInfoListVO empSocInfo, List<SocDifferenceDO> differenceList, String head) {
        if (LATE_FEES.equals(head)) {
            BigDecimal socialSecurityFine = ObjectUtils.isEmpty(empSocInfo.getSocialSecurityFine()) ?
                    new BigDecimal("0") : empSocInfo.getSocialSecurityFine();
            BigDecimal accumulationFundFine = ObjectUtils.isEmpty(empSocInfo.getAccumulationFundFine()) ?
                    new BigDecimal("0") : empSocInfo.getAccumulationFundFine();
            return socialSecurityFine.add(accumulationFundFine).toPlainString();
        } else if (SERVICE_COST.equals(head)) {
            BigDecimal serviceCost = ObjectUtils.isEmpty(empSocInfo.getServiceCost()) ?
                    new BigDecimal("0") : empSocInfo.getServiceCost();
            return serviceCost.stripTrailingZeros().toPlainString();
        } else {
            if (CollectionUtils.isEmpty(differenceList)) {
                return "";
            }
            for (SocDifferenceDO socDifference : differenceList) {
                if (head.contains(socDifference.getDifferenceName())) {
                    if (head.contains(COMPANY)) {
                        return socDifference.getCompanyDifference().toPlainString();
                    } else if (head.contains(PERSONAL)) {
                        return socDifference.getPersonalDifference().toPlainString();
                    } else {
                        return "";
                    }
                }
            }
            return "";
        }
    }

    /**
     * 缴费合计
     */
    public static String totalConversion(EmpSocInfoListVO empSocInfo, List<SocDifferenceDO> differenceList, String head) {
        if (SOC_STATS.equals(head)) {
            return empSocInfo.getCompanySocAmount().add(empSocInfo.getPersonalSocAmount())
                    .stripTrailingZeros().toPlainString();
        } else if (PF_STATS.equals(head)) {
            return empSocInfo.getCompanyPfAmount().add(empSocInfo.getPersonalPfAmount())
                    .stripTrailingZeros().toPlainString();
        } else if (SOC_PF_STATS.equals(head)) {
            return empSocInfo.getCompanySocAmount().add(empSocInfo.getCompanyPfAmount())
                    .add(empSocInfo.getPersonalSocAmount()).add(empSocInfo.getPersonalPfAmount())
                    .stripTrailingZeros().toPlainString();
        } else if (RECEIVABLES_STATS.equals(head)) {
            BigDecimal differenceAmount = new BigDecimal("0");
            if (!CollectionUtils.isEmpty(differenceList)) {
                for (SocDifferenceDO socDifference : differenceList) {
                    differenceAmount = differenceAmount.add(socDifference.getPersonalDifference())
                            .add(socDifference.getCompanyDifference());
                }
            }
            BigDecimal serviceCost = ObjectUtils.isEmpty(empSocInfo.getServiceCost()) ?
                    new BigDecimal("0") : empSocInfo.getServiceCost();
            return empSocInfo.getCompanySocAmount().add(empSocInfo.getCompanyPfAmount())
                    .add(empSocInfo.getPersonalSocAmount()).add(empSocInfo.getPersonalPfAmount())
                    .add(serviceCost).add(empSocInfo.getLateFees())
                    .add(differenceAmount).stripTrailingZeros().toPlainString();
        } else {
            return "";
        }
    }

    /**
     * 子项合计
     */
    public static String itemConversion(String headTotal, List<EmpSocItemDO> infoItem, String head) {
        if (CollectionUtils.isEmpty(infoItem)) {
            return "";
        }
        for (EmpSocItemDO empSocItem : infoItem) {
            if (headTotal.equals(empSocItem.getItemName())) {
                if (head.contains(CARDINALITY)) {
                    return empSocItem.getCardinalityDefault().stripTrailingZeros().toPlainString();
                } else if (head.contains(COMPANY_PROPORTION)) {
                    return empSocItem.getPersentCompany().stripTrailingZeros().toPlainString() + "%";
                } else if (head.contains(PERSONAL_PROPORTION)) {
                    return empSocItem.getPersentPersonal().stripTrailingZeros().toPlainString() + "%";
                } else if (head.contains(COMPANY_AMOUNT)) {
                    return empSocItem.getAmountCompany().stripTrailingZeros().toPlainString();
                } else if (head.contains(PERSONAL_AMOUNT)) {
                    return empSocItem.getAmountPersonal().stripTrailingZeros().toPlainString();
                } else {
                    return "";
                }
            }
        }
        return "";
    }
}
